<template>
  <div>
    <el-card shadow="always" :body-style="{ padding: '20px' }">
      <CategorySelector :disabled="showState !== 1"></CategorySelector>
    </el-card>
    <el-card shadow="always" :body-style="{ padding: '20px' }" style="margin-top:20px">
      <SpuList v-if="showState === 1" @changeState="changeState"></SpuList>
      <SpuForm v-else-if="showState === 2" :spu="spu" @changeState="changeState"></SpuForm>
      <SkuForm v-else :spu="spu" @changeState="changeState"></SkuForm>
    </el-card>

  </div>
</template>
<script lang="ts">
import { defineComponent } from 'vue';
export default defineComponent({
  name: 'Spu'
})
</script>
<script lang="ts" setup>
import SpuList from './components/SpuList/index.vue'
import SpuForm from './components/SpuForm/index.vue'
import SkuForm from './components/SkuForm/index.vue'
import { ref } from 'vue'
import type { SpuData } from '@/api/spu'

const showState = ref(1)


// const spu = ref<SpuData>({
//   spuName: '',
//   description: '',
//   category3Id: '',
//   tmId: '',
//   spuSaleAttrList: [],
//   spuImageList: []
// })
const spu = ref<SpuData>({
  spuName: '',
  description: '',
  category3Id: '',
  tmId: '',
  spuSaleAttrList: [],
  spuImageList: []
})

// 定义自定义事件的回调
const changeState = (num: number, row: SpuData) => {
  showState.value = num
  if (row) {
    // 修改
    // 修改，需要把传递过来的row，再传递给spuForm界面进行回显
    //    此时我们就得定义一个数据保存row，否则没法传递给spuform，后期收集也是收集在这个有id的对象当中
    spu.value = row
  } else {
    // 添加,需要把spu进行重新初始化，也是要传递给spuForm，为了添加的时候收集数据
    spu.value = {
      spuName: '',
      description: '',
      category3Id: '',
      tmId: '',
      spuSaleAttrList: [],
      spuImageList: []
    }
  }
}
</script>

<style lang="less" scoped></style>
